package com.djf.djfword.controller;

import com.djf.djfword.entity.Employee;
import com.djf.djfword.service.AdvancedWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 高级Word文档操作控制器
 * 
 * @author djf
 */
@Controller
@RequestMapping("/advanced")
public class AdvancedWordController {

    @Autowired
    private AdvancedWordService advancedWordService;

    /**
     * 生成员工报告
     */
    @GetMapping("/employee-report")
    public void generateEmployeeReport(HttpServletResponse response) throws IOException {
        // 模拟员工数据
        List<Employee> employees = createSampleEmployees();
        advancedWordService.generateEmployeeReport(response, employees);
    }

    /**
     * 生成合同模板
     */
    @GetMapping("/contract")
    public void generateContract(HttpServletResponse response,
                               @RequestParam(defaultValue = "张三") String employeeName,
                               @RequestParam(defaultValue = "Java开发工程师") String position,
                               @RequestParam(defaultValue = "12000.00") Double salary) throws IOException {
        LocalDate startDate = LocalDate.now().plusDays(7); // 一周后入职
        advancedWordService.generateContractTemplate(response, employeeName, position, salary, startDate);
    }

    /**
     * 生成销售图表报告
     */
    @GetMapping("/chart-report")
    public void generateChartReport(HttpServletResponse response) throws IOException {
        advancedWordService.generateChartStyleDocument(response);
    }

    /**
     * 自定义员工报告
     */
    @PostMapping("/custom-employee-report")
    public void generateCustomEmployeeReport(HttpServletResponse response,
                                           @RequestBody List<Employee> employees) throws IOException {
        if (employees == null || employees.isEmpty()) {
            employees = createSampleEmployees();
        }
        advancedWordService.generateEmployeeReport(response, employees);
    }

    /**
     * 批量生成合同
     */
    @PostMapping("/batch-contracts")
    @ResponseBody
    public Map<String, Object> batchGenerateContracts(@RequestBody List<Employee> employees) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 这里可以实现批量生成合同的逻辑
            // 实际项目中可以将生成的文件保存到服务器，然后返回下载链接
            
            result.put("success", true);
            result.put("message", "批量合同生成完成");
            result.put("count", employees.size());
            result.put("note", "实际项目中可以将文件保存到服务器并提供下载链接");
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量生成失败：" + e.getMessage());
        }
        
        return result;
    }

    /**
     * 获取员工数据模板（用于前端参考）
     */
    @GetMapping("/employee-template")
    @ResponseBody
    public List<Employee> getEmployeeTemplate() {
        return createSampleEmployees();
    }

    /**
     * 创建示例员工数据
     */
    private List<Employee> createSampleEmployees() {
        List<Employee> employees = new ArrayList<>();
        
        employees.add(new Employee(
            1L, "张三", 28, "Java开发工程师", "技术部", 
            12000.0, LocalDate.of(2022, 3, 15), 
            "zhangsan@example.com", "13800138001", "北京市朝阳区"
        ));
        
        employees.add(new Employee(
            2L, "李四", 25, "前端开发工程师", "技术部", 
            10000.0, LocalDate.of(2022, 6, 20), 
            "lisi@example.com", "13800138002", "北京市海淀区"
        ));
        
        employees.add(new Employee(
            3L, "王五", 32, "项目经理", "项目部", 
            18000.0, LocalDate.of(2021, 8, 10), 
            "wangwu@example.com", "13800138003", "北京市西城区"
        ));
        
        employees.add(new Employee(
            4L, "赵六", 26, "测试工程师", "质量部", 
            9000.0, LocalDate.of(2023, 1, 5), 
            "zhaoliu@example.com", "13800138004", "北京市东城区"
        ));
        
        employees.add(new Employee(
            5L, "钱七", 30, "产品经理", "产品部", 
            15000.0, LocalDate.of(2021, 12, 1), 
            "qianqi@example.com", "13800138005", "北京市丰台区"
        ));
        
        employees.add(new Employee(
            6L, "孙八", 27, "UI设计师", "设计部", 
            11000.0, LocalDate.of(2022, 9, 18), 
            "sunba@example.com", "13800138006", "北京市石景山区"
        ));
        
        employees.add(new Employee(
            7L, "周九", 29, "运维工程师", "技术部", 
            13000.0, LocalDate.of(2022, 4, 25), 
            "zhoujiu@example.com", "13800138007", "北京市通州区"
        ));
        
        employees.add(new Employee(
            8L, "吴十", 24, "数据分析师", "数据部", 
            10500.0, LocalDate.of(2023, 2, 14), 
            "wushi@example.com", "13800138008", "北京市昌平区"
        ));
        
        return employees;
    }
}